var dtFmlPhoneInput = $("#dtFmlPhone");
var dtFmlVerifyCodeInput = $("#dtFmlVerifyCode");
var dtFmlNickNameInput = $("#dtFmlNickName");
var dtFmlPwdInput = $("#dtFmlPwd");
var dtFmlPwdInput2 = $('#dtFmlPwd2');

var sendVerifyCodeBtn = $("#sendVerifyCode");
var registerSubmitBtn = $("#dtFmlRegisterSubmit");

var dtLoginPhoneInput = $('#dtLoginPhone');
var dtLoginPwdInput = $('#dtLoginPwd');
var loginBtn = $("#dtFmlLoginBtn");//登陆跳转按钮
var registerBtn = $('#registerBtn');//注册跳转按钮
var loginForgetPwd = $('#dtFmlForgetPwd');//忘记密码
var forgetVerifyCodeBtn = $('#sendForgetVerifyCode');//忘记密码 获取短信验证码
var loginSubmitBtn = $("#dtFmlLoginSubmit");//登陆提交按钮
var pwdSubmitBtn = $('#dtFmlPwdSubmit');//确定修改密码

var verifyTimer = null;//计时器
var curCount = 0;
//注册信息
var registerData = {
    //inviteCode: '',
    phone: '',
    password: '',
    nickName: '',
    verifyCode: ''
}
/**
 * 手机号输入期间检查（只允许输入数字）
 */
function check(e){
    var keyCode = e.keyCode || event.keyCode;
    if (keyCode >= 48 && keyCode <= 57) {
        event.returnValue = true;
    }else {
        event.returnValue = false;
    }
}
/**
 * 输入时字体颜色变深
 */
var inputFocus = function(input){
    $(input).css({ color: '#88888d'});
}
/**
 * 不在输入状态&内容为空时 字体颜色变浅
 */
var inputBlur = function(input){
    var val = $(input).val().trim();
    if(val==''){
        $(input).css({ color: '#c9c9c9'});
    }
}
/**
 * 非空检查
 */
var notNullCheck = function(value,hintName){
    if(value=="" || value==null){
        showMessageAlert("请输入"+hintName+"");
        return false;
    }
    return true;
}
/**
 * 手机号码长度检查
 */
var mobilePhoneCheck = function(value,focusInput){
    if(value.length!=11) {
        showMessageAlert("请输入11位手机号码");
        focusInput.focus();
        return false;
    }
    registerData.phone = value;
    return true;
}
/**
 * 手机号码验证
 */
var mobilePhoneSubmitCheck = function(){
    var inputPhone = dtFmlPhoneInput.val().trim();
    var savePhone = registerData.phone;
    if(inputPhone=="" || inputPhone==null){
        showMessageAlert("请输入手机号码");
        return false;
    }else if(inputPhone.length!=11) {
        showMessageAlert("请输入11位手机号码");
        dtFmlPhoneInput.focus();
        return false;
    }else if((savePhone=="" || savePhone==null) || savePhone==inputPhone){
        registerData.phone = inputPhone;
        return true;
    }else if(inputPhone != savePhone){
        showMessageAlert("请验证手机号码");
        return false;
    }
}
/**
 * 用户名/昵称校验
 */
var nickNameCheck = function(){
    var value = dtFmlNickNameInput.val().trim();
    var reg = /[\u4e00-\u9fa5|a-zA-Z|0-9]/;
    if(value=="" || value==null){
        showMessageAlert("请输入昵称");
        dtFmlNickNameInput.focus();
        return false;
    }else if(!reg.test(value)){
        showMessageAlert("昵称不可输入特殊符号");
        dtFmlNickNameInput.focus();
        return false;
    }
    registerData.nickName = value;
    return true;
}
/**
 * 验证码输入框检查
 */
var verifyInputCheck = function(){
    var value = dtFmlVerifyCodeInput.val().trim();
    if(value!=null && value!="" && value.length>0){
        if(value.length==4){
            registerData.verifyCode = value;
            return true;
        }else{
            showMessageAlert("请输入4位验证码");
            dtFmlVerifyCodeInput.focus();
            return false;
        }
    }else{
        showMessageAlert("请输入验证码");
        dtFmlVerifyCodeInput.focus();
        return false;
    }
}
/**
 * 密码格式检查
 */
var pwdInputCheck = function(){
    var value = dtFmlPwdInput.val().trim();
    if(value!=null && value!="" && value.length>0){
        if(value.length<6){
            showMessageAlert("密码长度必须大于6位");
            dtFmlPwdInput.focus();
            return false;
        }
        registerData.password = value;
        return true;
    }else{
        showMessageAlert("请输入6~16位密码");
        dtFmlPwdInput.focus();
        return false;
    }
}
var pwd2InputCheck = function(){
    var value = dtFmlPwdInput.val().trim();
    var value2 = dtFmlPwdInput2.val().trim();
    if(value==null || value=="" || value.length==0){
        showMessageAlert("请输入新密码");
        return false;
    }else if(value.length<6){
        showMessageAlert("密码长度必须大于6位");
        dtFmlPwdInput.focus();
        return false;
    }else if(value.length>16){
        showMessageAlert("密码长度必须小于16位");
        dtFmlPwdInput.focus();
        return false;
    }else if(value2==null || value2=="" || value2.length==0){
        showMessageAlert("请再次确认新密码");
        return false;
    }else if(value != value2){
        showMessageAlert("请确认两次密码一致");
        return false;
    }else{
        registerData.password = value;
        return true;
    }
}
/**
 * 注册信息提交
 */
var registerInfoCheck = function(){
    if(!mobilePhoneSubmitCheck()){
        return false;
    }else if(!pwdInputCheck()){
        return false;
    }else if(!nickNameCheck()){
        return false;
    }else if(!verifyInputCheck()){
        return false;
    }
    var invitedCode = $('#dtFmlInvitedCode');
    if(invitedCode && invitedCode.length>0){
        registerData.inviteCode = $('#dtFmlInvitedCode').val().trim();
    }
    registerSubmitBtn.addClass('bg-light');
    registerSubmitBtn.off("click",registerInfoCheck);
    $.ajax({
        type: 'POST',
        url: getAjaxUrl('/dog2/appUser/register',registerData),
        //url: '/dog2/appUser/register',
        //data: paramCoding(registerData),
        dataType: 'json',
        success: function(res){
            if(res.respCode==0){
                showMessageAlert("注册成功");
                //用户登陆信息
                var info = '?phone='+res.phone+'&userId='+res.userId+'&nickname='+res.nickName+'&session='+res.session;
                //注册成功跳转到其他页面
                setTimeout(function(){
					location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaaecc90537464f29&redirect_uri='+encodeURIComponent('http://www.viptail.com/shop/shop.html'+info)+'&response_type=code&scope=snsapi_base#wechat_redirect';
					// location.href = 'shop.html';
                },500);
            }else{
                showMessageAlert(res.respDesc,11);
                registerSubmitBtn.removeClass('bg-light');
                registerSubmitBtn.on("click",registerInfoCheck);
            }
            if(res.errResult){
                showMessageAlert(res.errMsg,11);
                registerSubmitBtn.removeClass('bg-light');
                registerSubmitBtn.on("click",registerInfoCheck);
            }
        },
        error: function(){
            showMessageAlert('提交注册信息连接出错');
            registerSubmitBtn.removeClass('bg-light');
            registerSubmitBtn.on("click",registerInfoCheck);
        }
    });
}
/**
 * 倒计时
 * @param count
 * @param element
 */
var countDown = function(sendBtn){
    curCount--;
    if(curCount == 0){
        return false;
    }
    sendBtn.text((curCount-1)+'秒');
    return true;
}
/**
 * 点击发送 获取短信验证码
 */
var getVerifyCode = function(data,url,sendingEvent,sendBtn){
    sendBtn.attr('disabled',true);//禁止获取验证码按钮
    sendBtn.off("click",sendingEvent);//移除事件
    sendBtn.addClass("disabled");
    $.ajax({
        type: 'POST',
        url: getAjaxUrl(url,data),
        //url: '/dog2/appUser/foundPassword',
        //data: paramCoding(data),
        dataType: 'json',
        success: function(res){
            if(res.respCode==0){
                showMessageAlert("验证码已发送");
                curCount = 60;
                sendBtn.text((curCount-1)+'秒');
                verifyTimer = setInterval(function(){
                    if(!countDown(sendBtn)){
                        clearInterval(verifyTimer);
                        sendBtn.attr('disabled',false);
                        sendBtn.on("click",sendingEvent);
                        sendBtn.removeClass("disabled");
                        sendBtn.text('发送验证码');
                    }
                },1000);
            }else{
                if(res.respDesc){
                    showMessageAlert(res.respDesc,11);
                }else{
                    showMessageAlert('发送失败');
                }
                sendBtn.attr('disabled',false);
                sendBtn.on("click",sendingEvent);
                sendBtn.removeClass("disabled");
                sendBtn.text('发送验证码');
            }
            if(res.errResult){
                showMessageAlert(res.errMsg,11);

                sendBtn.attr('disabled',false);
                sendBtn.on("click",sendingEvent);
                sendBtn.removeClass("disabled");
                sendBtn.text('发送验证码');
            }
        },
        error: function(){
            showMessageAlert('发送验证码连接出错');

            sendBtn.attr('disabled',false);
            sendBtn.on("click",sendingEvent);
            sendBtn.removeClass("disabled");
            sendBtn.text('发送验证码');
        }
    });
};
var sendingVerifyCode = function(){
    var phone = dtFmlPhoneInput.val().trim();
    if(notNullCheck(phone,"手机号码")){
        if(mobilePhoneCheck(phone,dtFmlPhoneInput)){
            //如果手机号码非空
            var data = {
                phone: phone
            }
            getVerifyCode(data,'/dog2/mobile/sendRegSmsVerify',sendingVerifyCode,sendVerifyCodeBtn);
        }
    }
}
/**
 * 发送忘记密码验证短信
 */
var sendForgetVerifyCode = function(){
    var phone = $('#dtPwdPhone').val().trim();
    if(notNullCheck(phone,"手机号码")){
        if(phone.length!=11) {
            showMessageAlert("请输入11位手机号码");
            return false;
        }else{
            var data = {
                phone: phone
            }
            getVerifyCode(data,'/dog2/mobile/sendForgetSmsVerify',sendForgetVerifyCode,forgetVerifyCodeBtn);
        }
    }
}
/**
 * 跳转到登陆界面
 */
var login = function(){
    window.open("login.html","_self");
}
/**
 * 忘记密码
 */
var forgetPwd = function(){
    window.open("pwd.html","_self");
}
/**
 * 注册
 */
var register = function(){
    window.open("register.html","_self");
}
/**
 * 登陆提交按钮
 */
var loginSubmit = function(){
    //检查用户名、密码输入
    var phone = dtLoginPhoneInput.val().trim();
    var pwd = dtLoginPwdInput.val().trim();
    if(phone=='' || phone==null){
        showMessageAlert('请输入手机号');
        return false;
    }else if(pwd=='' || pwd==null){
        showMessageAlert('请输入密码');
        return false;
    }
    //获取设备信息
    var hardwareinfo = '';//硬件信息
    var resolution = screen.width+'*'+screen.height;//硬件分辨率
    var systype = JAE.util.ua.os;
    var sysversion = '';
    if(JAE.util.ua.ios){
        //ios
        sysversion = JAE.util.ua.ios+'';
        if(JAE.util.ua.iphone){
            hardwareinfo = 'iphone';
        }else if(JAE.util.ua.ipad){
            hardwareinfo = 'ipad';
        }else if(JAE.util.ua.ipod){
            hardwareinfo = 'ipod';
        }
    }else if(JAE.util.ua.android){
        //android
        sysversion = JAE.util.ua.android+'';
        hardwareinfo = 'android'
    }
    var loginData = {
        appversion: 'web',
        hardwareinfo: hardwareinfo,
        password: pwd,
        phone: phone,
        resolution: resolution,
        systype: systype,
        sysversion: sysversion
    }
    loginSubmitBtn.addClass("bg-light");
    loginSubmitBtn.off("click",loginSubmit);
    $.ajax({
        type: 'POST',
        url: getAjaxUrl('/dog2/appUser/login',loginData),
        //url: '/dog2/appUser/login',
        //data: paramCoding(loginData),
        dataType: 'json',
        success: function(res){
            if(res.respCode==0){
                showMessageAlert("登录成功");
                //本地存储 用户登录信息
                var info = '?phone='+res.phone+'&userId='+res.userId+'&nickname='+res.nickName+'&session='+res.session;
                //登陆成功后跳转至其他页面
                setTimeout(function(){
                    location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaaecc90537464f29&redirect_uri='+encodeURIComponent('http://www.viptail.com/shop/shop.html'+info)+'&response_type=code&scope=snsapi_base#wechat_redirect';
                	// location.href = 'shop.html';
                },500);
            }else{
                showMessageAlert(res.respDesc,11);
                loginSubmitBtn.removeClass("bg-light");
                loginSubmitBtn.on("click",loginSubmit);
            }
            if(res.errResult){
                showMessageAlert(res.errMsg,11);
                loginSubmitBtn.removeClass("bg-light");
                loginSubmitBtn.on("click",loginSubmit);
            }
        },
        error: function(){
            showMessageAlert('用户登录连接出错');
            loginSubmitBtn.removeClass("bg-light");
            loginSubmitBtn.on("click",loginSubmit);
        }
    });
}
var pwdSubmit = function(){
    var pwdData = {
        newPassword: $('#dtFmlPwd').val().trim(),
        phone: $('#dtPwdPhone').val().trim(),
        verifyCode: $('#dtPwdVerifyCode').val().trim()
    }
    if(!notNullCheck(pwdData.phone,'手机号')){
        return false;
    }else if(!notNullCheck(pwdData.verifyCode,'验证码')){
        return false;
    }else if(!pwd2InputCheck()){
        return false;
    }
    pwdSubmitBtn.addClass('bg-light');
    pwdSubmitBtn.off("click",pwdSubmit);
    $.ajax({
        url: getAjaxUrl('/dog2/appUser/foundPassword',pwdData),
        //url: '/dog2/appUser/foundPassword',
        //data: paramCoding(pwdData),
        type: 'POST',
        dataType: 'json',
        success: function(res){
            if(res.respCode==0){
                showMessageAlert('修改成功');
                window.open("login.html","_self");
            }else{
                showMessageAlert(res.respDesc,11);
                pwdSubmitBtn.removeClass('bg-light');
                pwdSubmitBtn.on("click",pwdSubmit);
            }
            if(res.errResult){
                showMessageAlert(res.errMsg,11);
                pwdSubmitBtn.removeClass('bg-light');
                pwdSubmitBtn.on("click",pwdSubmit);
            }
        },
        error: function(){
            showMessageAlert('提交修改密码信息连接出错');
            pwdSubmitBtn.removeClass('bg-light');
            pwdSubmitBtn.on("click",pwdSubmit);
        }
    });
}
/**
 * 按钮绑定事件
 */
registerSubmitBtn.on("click",registerInfoCheck);//注册信息提交
loginBtn.on("click",login);//跳转到登录界面
registerBtn.on("click",register);//跳转到注册
loginForgetPwd.on("click",forgetPwd);//忘记密码
pwdSubmitBtn.on("click",pwdSubmit);//提交新密码
forgetVerifyCodeBtn.on("click",sendForgetVerifyCode);//忘记密码发送验证码
loginSubmitBtn.on("click",loginSubmit);//登录信息提交
sendVerifyCodeBtn.on("click",sendingVerifyCode);//发送验证码
dtFmlVerifyCodeInput.on("change",verifyInputCheck);//验证码检查
/**
 * 微信
 */
if(isWechat()){
    $.base64.utf8encode = true;
    var wxData = {
        url: $.base64.encode(location.href),
        base64: true
    };
    $.ajax({
        type: 'POST',
        async: false,
        dataType: 'json',
        url: getAjaxUrl('/dog2/wechatComm/weChatConfig',wxData),
        //url: '/dog2/wechatComm/weChatConfig',
        //data: paramCoding(wxData),
        success: function (data) {
            wx.config({
                appId: data.appId,
                timestamp: data.timestamp,
                nonceStr: data.nonceStr,
                signature: data.signature,
                jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone']
            });
            if(data.errResult){
                showMessageAlert(data.errMsg,11);
            }
        },
        error: function (data) {
            data = $.parseJSON(data['responseText']);
            showMessageAlert(data['message']);
        },
        cache: false
    });
    //分享
    var imgUrl = 'http://www.viptail.com/shop/dist/img/logo_up_down.png';
    var desc = '6月自驾游 | 带着狗狗看世界，苏州吴江2日游开始报名啦';
    var link = 'http://www.viptail.com/shop/shop.html';
    wx.ready(function () {
        //分享到朋友圈
        wx.onMenuShareTimeline({
            title: '小狗在家-'+desc,
            link: link,
            imgUrl: imgUrl
        });
        //分享给朋友
        wx.onMenuShareAppMessage({
            desc: desc,
            link: link,
            imgUrl: imgUrl
        });
        //分享到QQ
        wx.onMenuShareQQ({
            desc: desc,
            link: link,
            imgUrl: imgUrl
        });
        //分享到腾讯微博
        wx.onMenuShareWeibo({
            desc: desc,
            link: link,
            imgUrl: imgUrl
        });
        //分享到腾讯微博
        wx.onMenuShareQZone({
            desc: desc,
            link: link,
            imgUrl: imgUrl
        });
    });
}
/**
 * 获取设备信息
 */
!function(a) {
    function b(a) {
        var b = 0;
        return parseFloat(a.replace(/\./g,
            function() {
                return 0 === b++?".": ""
            }))
    }
    var c = {},
        d = a.navigator,
        e = d && d.userAgent || "",
        f = {
            os: void 0,
            mobile: void 0,
            android: void 0,
            ios: void 0,
            iphone: void 0,
            ipad: void 0,
            ipod: void 0
        }
    c.depend = {
        android: "3.99",
        ios: "3.99"
    },
        function() {
            var a;
            / Mobile\//.test(e) && e.match(/iPad|iPod|iPhone/) ? (f.mobile = "apple", a = e.match(/OS ([^\s]*)/), a && a[1] && (f.ios = b(a[1].replace("_", "."))), f.os = "ios", a = e.match(/iPad|iPod|iPhone/), a && a[0] && (f[a[0].toLowerCase()] = f.ios)) : / Android/i.test(e) && (/Mobile/.test(e) && (f.os = f.mobile = "android"), a = e.match(/Android ([^\s]*);/), a && a[1] && (f.android = b(a[1])))
        } (),
        c.util = {
            ua: f
        },
        a.JAE = c
} (window);
